package com.example.sspulibrary.db;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.util.Log;

import com.example.sspulibrary.model.Book;
import com.example.sspulibrary.model.ExceptBook;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
 * @author : HengZhang
 * <p>
 * 数据库的Cursor转换成实体类
 */

public class DBTools {

    private static final String TAG = "DBTools";

    public boolean checkCursor(Cursor cursor) {
        int resultCounts = cursor.getCount();
        if (resultCounts == 0 || !cursor.moveToFirst()) {
            return false;
        }
        return true;
    }

    @SuppressLint("Range")
    public List<ExceptBook> convertToExceptBooks(Cursor cursor, String EXCEPT_RFID,
                                                 String EXCEPT_BookName, String EXCEPT_CurrentTime,
                                                 String EXCEPT_CurrentLocation,
                                                 String EXCEPT_RightLocation) {
        if (!checkCursor(cursor)) {
            return Collections.emptyList();
        }
        ArrayList<ExceptBook> exceptBooks = new ArrayList<>();

        if (cursor.moveToFirst()) {
            for (int i = 0; i <  cursor.getCount(); i++){
                ExceptBook exceptBook = new ExceptBook();
                exceptBook.setBookRFID((cursor.getString(cursor.getColumnIndex(EXCEPT_RFID))));
                exceptBook.setBookName((cursor.getString(cursor.getColumnIndex(EXCEPT_BookName))));
                exceptBook.setCurrentTime((cursor.getString(cursor.getColumnIndex(EXCEPT_CurrentTime))));
                exceptBook.setCurrentLocation((cursor.getString(cursor.getColumnIndex(EXCEPT_CurrentLocation))));
                exceptBook.setRightLocation((cursor.getString(cursor.getColumnIndex(EXCEPT_RightLocation))));

                exceptBooks.add(exceptBook);
                cursor.moveToNext();
            }

        }
        return exceptBooks;
    }

    @SuppressLint("Range")
    public List<Book> convertToBooks(Cursor cursor, String ALL_RFID, String ALL_BarCode, String ALL_Name,
                                    String ALL_BookShelf, String ALL_StoreHouse, String ALL_Row,
                                    String ALL_Face, String ALL_Series, String ALL_CreateTime, String ALL_UpdateTime,
                                    String ALL_CurrentTime) {
        if (!checkCursor(cursor)) {
            return Collections.emptyList();
        }
        ArrayList<Book> books = new ArrayList<>();
        if (cursor.moveToFirst()) {

            for (int i = 0; i <  cursor.getCount(); i++){
                Book book = new Book();
                book.setBookRFID(cursor.getString(cursor.getColumnIndex(ALL_RFID)));
                book.setBookBarCode(cursor.getString(cursor.getColumnIndex(ALL_BarCode)));
                book.setBookName(cursor.getString(cursor.getColumnIndex(ALL_Name)));
                book.setBookShelf(cursor.getString(cursor.getColumnIndex(ALL_BookShelf)));
                book.setStoreHouse(cursor.getString(cursor.getColumnIndex(ALL_StoreHouse)));
                book.setRow(cursor.getString(cursor.getColumnIndex(ALL_Row)));
                book.setFace(cursor.getString(cursor.getColumnIndex(ALL_Face)));
                book.setSeries(cursor.getString(cursor.getColumnIndex(ALL_Series)));
                book.setCreateTime(cursor.getString(cursor.getColumnIndex(ALL_CreateTime)));
                book.setUpdateTime(cursor.getString(cursor.getColumnIndex(ALL_UpdateTime)));
                book.setCurrentTime(cursor.getString(cursor.getColumnIndex(ALL_CurrentTime)));

                books.add(book);
                cursor.moveToNext();
            }
        }
        return books;
    }
}
